package sif.Activity;

import sif.Entity.MainMenuItem;
import sif.FacebookSDK.AsyncFacebookRunner;
import sif.FacebookSDK.DialogError;
import sif.FacebookSDK.Facebook;
import sif.FacebookSDK.FacebookError;
import sif.FacebookSDK.Facebook.DialogListener;
import sif.Static.FacebookObjects;
import sif.Static.SaveData;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainMenuActivity extends Activity {
	FacebookObjects fbObjects;
	
	public void initComponents(){
		fbObjects = FacebookObjects.getFacebookObjects();
		
		//login facebook
		loginToFacebook();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_menu);
		
		initComponents();
		
		MainMenuAdapter adapter = new MainMenuAdapter(this, R.layout.row_main_menu);
		adapter.add(new MainMenuItem(R.drawable.home, "Home"));
		adapter.add(new MainMenuItem(R.drawable.friends, "Friends"));
		adapter.add(new MainMenuItem(R.drawable.new_feed1, "News Feed"));
		adapter.add(new MainMenuItem(R.drawable.profile, "Profile"));
		adapter.add(new MainMenuItem(R.drawable.album, "Album"));
		GridView gvMainMenu = (GridView) findViewById(R.id.gvMainMenu);
		gvMainMenu.setAdapter(adapter);
		
		if(SaveData.getInstance(this).getAccessToken() != null){
			gvMainMenu.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View v,
						int position, long id) {
					switch (position) {
					case 0:
						
						break;
					case 1:
						Intent friendIntent = new Intent(MainMenuActivity.this, MyFriendActivity.class);
						startActivity(friendIntent);
						break;
					case 2:
						
						break;

					default:
						break;
					}
				}
			});
		}
	}
	
	/**
	 * Function to login into facebook
	 * */
	public void loginToFacebook() {

		String access_token = SaveData.getInstance(this).getAccessToken(); 
		long expires = SaveData.getInstance(this).getAccessExpire();

		Log.i("thaile", "access_token : " +  access_token);
		
		if (access_token != null) {
			fbObjects.getFacebook().setAccessToken(access_token);

			Log.i("thaile", "login successful");
			Log.d("FB Sessions", "" + fbObjects.getFacebook().isSessionValid());
		}
		
		if (expires != 0) {
			fbObjects.getFacebook().setAccessExpires(expires);
		}

		if (!fbObjects.getFacebook().isSessionValid()) {
			Log.i("thaile", "Starting login process");
			fbObjects.getFacebook().authorize(this, new String[] { "email", "publish_stream",
			"read_stream" }, new DialogListener() {

				@Override
				public void onCancel() {
					// Function to handle cancel event
				}
		
				@Override
				public void onComplete(Bundle values) {
					// Function to handle complete event
					// Edit Preferences and update facebook acess_token
					SaveData.getInstance(MainMenuActivity.this).setAccessToken(fbObjects.getFacebook().getAccessToken());
					SaveData.getInstance(MainMenuActivity.this).setAccessExpire(fbObjects.getFacebook().getAccessExpires());
		
					Log.i("thaile", "login successful");
				}
		
				@Override
				public void onError(DialogError error) {
					Log.i("thaile", "login fail");
				}
		
				@Override
				public void onFacebookError(FacebookError fberror) {
					Log.i("thaile", "login fail");
				}
		
			});	
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		fbObjects.getFacebook().authorizeCallback(requestCode, resultCode, data);
	}
	
	class MainMenuAdapter extends ArrayAdapter<MainMenuItem>{

		public MainMenuAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if(v == null){
				LayoutInflater inflater =  getLayoutInflater();
				v = inflater.inflate(R.layout.row_main_menu, null);
			}
			((ImageView)v.findViewById(R.id.ivMainMenuIcon)).setImageResource(this.getItem(position).getIcon());
			((TextView)v.findViewById(R.id.tvMainMenuName)).setText(this.getItem(position).getName());
			return v;
		}
	}
}
